#include <unistd.h>
#include <stdio.h>
#include "config.h"
#include "mailmsg.h"

#ifndef SENDMAIL_CMD
#define SENDMAIL_CMD "/usr/lib/sendmail -odb -oi -t"
#endif

FILE *
Mail_Open(void)
{
  return tmpfile();
}

int
Mail_Send(FILE *fp, const char *pstrFrom,
          const char *pstrTo, const char *pstrSubj)
{
  FILE *fpMail=popen(SENDMAIL_CMD, "w");

  fflush(fp);

  if(fpMail)
  {
    char buffer[512];

    fprintf(fpMail, "From: %s\n", pstrFrom);
    fprintf(fpMail, "To: %s\n", pstrTo);
    fprintf(fpMail, "Subject: %s\n\n", pstrSubj);

    rewind(fp);
    while(!feof(fp))
    {
      int bytesread=fread(buffer, 1, sizeof(buffer), fp);
      fwrite(buffer, 1, bytesread, fpMail);
    }
    pclose(fpMail);
  }
  return 0;
}

void
Mail_Close(FILE *fp)
{
  fclose(fp);
}
